﻿using System;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCUserDetail : System.Web.UI.UserControl
	{
		private IUserProvider _UserProvider;

		public int UserID
		{
			get
			{
				return CommonHelper.QueryStringInt("userid");
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				var u = SiteContext.Current.CurrentUser;

				var permited = u != null && u.Role != null && (u.Role.RoleName.ToLower().Equals("admin") || u.Role.RoleName.ToLower().Equals("storemanager"));

				if(permited)
				{
					BindUser();
				}
			}
		}

		private void BindUser()
		{
			if(UserID > 0)
			{
				if(_UserProvider == null)
				{
					_UserProvider = new UserProvider();
				}

				var theUser = _UserProvider.GetUser(UserID, true);

				if(theUser != null)
				{
					this.ltrAddress.Text = string.Format("{0} {1} {2}", theUser.StateProvince != null ? theUser.StateProvince.Name : string.Empty, theUser.City != null ? theUser.City.CityName : string.Empty, theUser.Address);
					this.ltrBirthDate.Text = theUser.Birthdate.HasValue ? theUser.Birthdate.Value.ToShortDateString() : string.Empty;
					this.ltrCName.Text = theUser.UserCName;
					this.ltrPosition.Text = theUser.Position;
					this.ltrEMail.Text = theUser.Email;
					this.ltrFax.Text = theUser.Fax;
					this.ltrMSN.Text = theUser.MSN;
					this.ltrPhone.Text = theUser.PhoneNumber;
					this.ltrQQ.Text = theUser.QQ;
					this.ltrRemark.Text = theUser.Remark;
					if(theUser.Sex == 1)
					{
						this.ltrSex.Text = "男";
					}
					else if(theUser.Sex == 2)
					{
						this.ltrSex.Text = "女";
					}
					else if(theUser.Sex == 0)
					{
						this.ltrSex.Text = "保密";
					}
					this.ltrSFZ.Text = theUser.ShengFenZheng;
					this.ltrTel.Text = theUser.Telephone;
					this.ltrUserName.Text = theUser.UserName;
					this.ltrZip.Text = theUser.ZipCode;

					if(theUser.UserState == (int)UserState.Active)
					{
						this.ltrUserState.Text = "有效";
					}
					else
					{
						this.ltrUserState.Text = "已删除";
					}

					ltrCredits.Text = theUser.Credits.GetValueOrDefault(0).ToString();

					ltrSecretQuestion1.Text = theUser.SecretQuestion1;
					ltrSecretQuestion2.Text = theUser.SecretQuestion2;
					ltrSecretAnswer1.Text = theUser.SecretAnswer1;
					ltrSecretAnswer2.Text = theUser.SecretAnswer2;
					ltrPwd.Text = theUser.UserPassword;
				}
			}
		}
	}
}